import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { CommService } from './../../comm/comm.service';
import 'rxjs/add/operator/map';
import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
import { ResourceAttribute } from '../../model/config/resource-attribute';
import { DataDictionaryDetail } from '../../model/sys/datadictionary-detail';
declare const $: any;
import { DxDataGridComponent, DxDropDownBoxComponent } from '../../../../../node_modules/devextreme-angular';
import { GlobalDataManager } from 'app/ptpmc/comm/global-data-manager';
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';

@Component({
    selector: 'app-resource-parttype',
    templateUrl: './parttype-cycletime.component.html',
})

export class PartTypeCycleTimeComponent implements OnInit, AfterViewInit {

    @ViewChild(DxDataGridComponent) dataGrid: DxDataGridComponent;
    /**选择项 */
    public items: ResourceAttribute[] = [];
    public selectedItems: ResourceAttribute[] = [];

     /*生产线 - 班次*/
     public subAreaItems: SubArea[];
     public subAreaSelected: any[];
    /*生产线 - 班次*/
    public equipmentTypeItems: DataDictionaryDetail[];
    public supplierItems: DataDictionaryDetail[];
    public boolSelectItems: any[];
    public equipemntItems: TVResource[] = [];
    public equipmentSelected: TVResource[];
    constructor(private commServices: CommService) {
    }

    @ViewChild('ddda') dddaS: DxDropDownBoxComponent
    @ViewChild('dddb') dddb: DxDropDownBoxComponent
    clearSubAreaSelected() {
        this.subAreaSelected = [];
    }
    subAreaSelectedOk() {
        this.dddaS.instance.close();
    }
    clearEquipmentSelected() {
        this.equipmentSelected = [];
    }
    equipmentSelectedOk() {
        this.dddb.instance.close();
    }

    ngOnInit() {
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAreaSelected = [];
        this.equipemntItems = [];
        this.subAreaSelected.forEach(e => {
            const arr = this.commServices.getTVResourceBySubArea(e.subArea);
            arr.forEach(a => { this.equipemntItems.push(a); });
        });
        this.equipmentSelected = [];
    }

    onSubAreaSelectedChange(subArea: any): void {
        this.equipemntItems = [];
        this.subAreaSelected.forEach(e => {
            const arr = this.commServices.getTVResourceBySubArea(e.subArea);
            arr.forEach(a => { this.equipemntItems.push(a); });
        });

        this.equipmentSelected = [];

    }

    ngAfterViewInit() {
    }

    /**获取表格数据*/
    getReportData(): void {
        const queryModel: QueryModel = new QueryModel();
        queryModel.querySubAreaIds = this.subAreaSelected.map(e => e.subArea);
        this.equipmentSelected
        debugger
        queryModel.queryResourceIds = this.equipmentSelected.map(e => e.equipment);
        this.commServices
            .getList(queryModel, '/partct/list')
            .subscribe(model => {
                this.items = [];
                if (model.resultCode === 200) {
                    this.items = model.data;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }
    refresh(): void {
        this.getReportData();
    }
    
    /**
     * 保存更新值到数据库
     */
    saveChanges() {
        this.updatePartTypeList
        debugger
        this.commServices.update(this.updatePartTypeList, '/partct/updatep')
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.updatePartTypeList = [];
                    alert('更新成功！');
                } else {
                    alert('更新失败！' + model.resultMessage);
                }
            });
    }

    public updatePartTypeList: any[] = [];
    updatePartType(e) {
        let index: number = this.updatePartTypeList.findIndex(u => u.ids === e.key.ids)
        if (index != -1) {
            this.updatePartTypeList[index] = e.key;
        } else {
            this.updatePartTypeList.push(e.key)
        }
    }
    

}
